﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vibz.SharePoint
{
    public class SPSiteData
    {
        SiteData.SiteData _siteData;
        public SPSiteData(string siteUrl)
        {
            _siteData = new Vibz.SharePoint.SiteData.SiteData();
            _siteData.Url = siteUrl + "/_vti_bin/sitedata.asmx";
            _siteData.UseDefaultCredentials = true;
        }
        public static bool IsValidSite(string siteUrl)
        {
            try
            {
                //SiteData.SiteData siteData = new Vibz.SharePoint.SiteData.SiteData();
                //siteData.Url = siteUrl;
                //siteData.UseDefaultCredentials = true;
                //string strSite = String.Empty;
                //string strWeb = String.Empty;

                //siteData.GetSiteAndWeb(siteUrl, out strSite, out strWeb);
                return true;
            }
            catch(Exception exc)
            {
                Console.WriteLine(DateTime.Now.ToString() + "\tIsValidSite - " + exc.Message);
                return false;
            }
        }
        public string[] GetAllList()
        {
            try
            {
                SiteData._sList[] lists;

                _siteData.GetListCollection(out lists);
                return lists.Select(lst => lst.Title).ToArray();
            }
            catch(Exception exc)
            {
                Console.WriteLine(DateTime.Now.ToString() + "\t GetAllList - " + exc.Message);
                return new string[] { };
            }
        }
        public string GetSiteOrWeb()
        {
            try
            {
                SiteData._sWebMetadata webMData;
                SiteData._sSiteMetadata siteMData;
                SiteData._sWebWithTime[] siteTime;
                SiteData._sListWithTime[] arrListWithTime;
                SiteData._sFPUrl[] arrUrls;
                string roles;
                string[] roleUsers;
                string[] roleGroups;

                string users;
                string groups;
                string[] arrGroups;

                uint i = _siteData.GetWeb(out webMData, out siteTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups);

                _siteData.GetSite(out siteMData, out siteTime, out users, out groups, out arrGroups);

                string strSite = String.Empty;
                string strWeb = String.Empty;

                _siteData.GetSiteAndWeb(_siteData.Url, out strSite, out strWeb);


                return strSite;
            }
            catch(Exception exc)
            {
                Console.WriteLine(DateTime.Now.ToString() + "\t GetSiteOrWeb - " + exc.Message);
                throw exc;
            }
        }
    }
}
